<script>
    export default {
        globalData: {
            latitude: '', //纬度，浮点数，范围为-90~90，负数表示南纬
            longitude: '', //经度，浮点数，范围为-180~180，负数表示西经
        },
        onLaunch: function() {
            let _this = this
            // console.log('App Launch');
            // #ifdef APP-PLUS
            
            // 语音合成
            var tts = uni.requireNativePlugin('nrb-tts-plugin');
            tts && tts.init({ lang: 'ZH', country: 'CN' }, res => {
                if (res.success == 0) {
                    console.log('初始化TTS成功', res);
                }
            });
            // 透传消息（在线） 注：有英文冒号时，数据格式会乱
            plus.push.addEventListener('receive', res => {
                let options = {cover: false };
                console.log('receive',res)
                if (res.payload) {
                    let type = res.payload.type
                    if (type == 1) {
                        uni.$emit('KEHU')
                        tts && tts.speak('您有新的报备信息', {}, e => {})
                    } else if (type == 2) {
                        uni.$emit('YJXX')
                        tts && tts.speak('您有新的佣金信息', {}, e => {})
                    } else if (type == 3) {
                        uni.$emit('DHFXX')
                        tts && tts.speak('您有新的待回访信息', {}, e => {})
                    } else if (type == 4) {
                        uni.$emit('WECHAT')
                        tts && tts.speak('您有新的微聊信息', {}, e => {})
                    } else if (type == 5) {
                        uni.$emit('RYSHXX')
                        tts && tts.speak('您有新的人员审核信息', {}, e => {})
                    } else if (type == 6) {
                        uni.$emit('NEWS')
                        tts && tts.speak('您有新消息', {}, e => {})
                    } else if (type == 7) {
                        uni.$emit('TEST_SH')
                        tts && tts.speak('您有新的试题审核信息', {}, e => {})
                    } else if (type == 8) {
                        uni.$emit('HIT_CARD')
                        tts && tts.speak('请及时打卡', {}, e => {})
                    } else if (type == 9) {
                        uni.$emit('ITEM_BAOBEI')
                        tts && tts.speak('您有新的项目报备信息', {}, e => {})
                    } else if (type == 10) {
                        uni.$emit('ITEM_BAOBEI')
                        tts && tts.speak('您有新的项目报备信息未审核', {}, e => {})
                    }
                    // plus.push.createMessage(res.payload.content,res.payload, options);
                    
                    plus.push.createMessage(res.content, res.payload, options);
                } else {
                    // 其他是后台发的，不会出现英文冒号，只有聊天时可能会出现英文冒号
                    uni.$emit('WECHAT')
                    tts && tts.speak('您有新的微聊信息', {}, e => {})
                    plus.push.createMessage(res.content);
                }
            }, false)
            
            // 点击消息栏推送消息，执行函数
            // 跳转页面 1报备 2佣金 3回访 4微聊 5人员 6请假 7活动 8打卡 9项目报备审核 10报备超过5分钟未审核
            plus.push.addEventListener("click", res => {
                console.log('click',res);
                if (res.payload) {
                    
                    // let a = res.payload.split('type')[1]
                    // let b = a.split(':')[1]
                    // let type = b.split(',')[0]
                    
                    let type = res.payload.type
                    if (type == 1) {
                        uni.switchTab({
                            url: '/pages/tabbar/kehu'
                        })
                    } else if (type == 2) {
                        uni.navigateTo({
                            url: '/mine/pages/record/commission'
                        })
                    } else if (type == 3) {
                        uni.navigateTo({
                            url: '/mine/pages/record/follow'
                        })
                    } else if (type == 4) {
                        uni.switchTab({
                            url: '/pages/tabbar/wechat'
                        })
                    } else if (type == 5) {
                        uni.switchTab({
                            url: '/pages/tabbar/mine'
                        })
                    } else if (type == 6) {
                        uni.switchTab({
                            url: '/pages/tabbar/news'
                        })
                    } else if (type == 7) {
                        uni.navigateTo({
                            url: '/staff/pages/opex-commissioner/opex-commissioner'
                        })
                    } else if (type == 8) {
                        uni.switchTab({
                            url: '/pages/tabbar/mine'
                        })
                    } else if (type == 9) {
                        uni.navigateTo({
                            url: '/staff/pages/shopowner/project-report'
                        })
                    } else if (type == 10) {
                        uni.navigateTo({
                            url: '/staff/pages/shopowner/project-report'
                        })
                    }
                } else {
                    uni.switchTab({
                        url: '/pages/tabbar/wechat'
                    })
                }
                plus.push.clear();
            },false)
            
            
            /**
             * 设置手机通知权限
             */
            if (plus.os.name == 'Android') { // 判断是Android
            	var main = plus.android.runtimeMainActivity();
            	var pkName = main.getPackageName();
            	var uid = main.getApplicationInfo().plusGetAttribute("uid");
            	var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
            	//android.support.v4升级为androidx
            	if (NotificationManagerCompat == null) {
            		NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
            	}
            	var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
            	// 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置  
            	if (!areNotificationsEnabled) {
            		uni.showModal({
            			title: '通知权限开启提醒',
            			content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
            			showCancel: false,
            			confirmText: '去设置',
            			success: function(res) {
            				if (res.confirm) {
            					var Intent = plus.android.importClass('android.content.Intent');
            					var Build = plus.android.importClass("android.os.Build");
            					//android 8.0引导  
            					if (Build.VERSION.SDK_INT >= 26) {
            						var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
            						intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
            					} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0  
            						var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
            						intent.putExtra("app_package", pkName);
            						intent.putExtra("app_uid", uid);
            					} else { //(<21)其他--跳转到该应用管理的详情页  
            						intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            						var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
            						intent.setData(uri);
            					}
            					// 跳转到该应用的系统通知设置页  
            					main.startActivity(intent);
            				}
            			}
            		});
            	}
            } else if (plus.os.name == 'iOS') { // 判断是ISO
            	var isOn = undefined;
            	var types = 0;
            	var app = plus.ios.invoke('UIApplication', 'sharedApplication');
            	var settings = plus.ios.invoke(app, 'currentUserNotificationSettings');
            	if (settings) {
            		types = settings.plusGetAttribute('types');
            		plus.ios.deleteObject(settings);
            	} else {
            		types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes');
            	}
            	plus.ios.deleteObject(app);
            	isOn = (0 != types);
            	if (isOn == false) {
            		uni.showModal({
            			title: '通知权限开启提醒',
            			content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
            			showCancel: false,
            			confirmText: '去设置',
            			success: function(res) {
            				if (res.confirm) {
            					var app = plus.ios.invoke('UIApplication', 'sharedApplication');
            					var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
            					plus.ios.invoke(app, 'openURL:', setting);
            					plus.ios.deleteObject(setting);
            					plus.ios.deleteObject(app);
            				}
            			}
            		});
            	}
            }

            // #endif
        },
        onShow: function() {
            // console.log('App Show')
        },
        onHide: function() {
            // console.log('App Hide')
        }
    }
</script>

<style lang="scss">
    @import "uview-ui/index.scss";
    // @import "/static/css/iconfont.css";
    @import url('@/static/css/main.css');

    /*每个页面公共css */
    image {
        width: auto;
        height: auto;
        display: block;
    }
    
    input[type="search"]{
        -webkit-appearance:none;
    }
    [type="search"]::-webkit-search-decoration {  
        display: none;  
    }
    input::-webkit-search-cancel-button {
        display: none;
    }
  
</style>